﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WF_QLThuVien.Entities.DauSach;
using WF_QLThuVien.Entities.Sach;
using WF_QLThuVien.Utilities;
using WF_QLThuVien.DataLayer.DauSach;

namespace WF_QLThuVien.BusinessLayer.DauSach
{
    public class CBLL_DauSach
    {
        CDAL_DauSach dalDauSach = new CDAL_DauSach();
        CDAL_Sach dalSach = new CDAL_Sach();

        public bool ThemDauSach(CDauSach ds)
        {
            try
            {
                if (dalDauSach.Insert(ds))
                {
                    ThemSach(ds);
                }
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi CBLL_DauSach.ThemDauSach \n" + ex.ToString());
            }
        }

        public bool ThemSach(CDauSach ds)
        {
            try
            {
                for (int i = 0; i < ds.SoLuong; i++)
                {
                    CSach s = new CSach();
                    s.SachID = ds.MaDauSach + i.ToString();
                    s.DauSachID = ds.MaDauSach;
                    s.TrangThai = ds.TrangThai;
                    dalSach.Insert(s);
                }
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi CBLL_DauSach.ThemSach \n" + ex.ToString());
            }
        }

        public bool XoaDauSach(CDauSach ds)
        {
            try
            {
                if (dalSach.DeleteByDauSachID(ds.MaDauSach))
                {
                    dalDauSach.Delete(ds.MaDauSach);
                    return true;
                }
                else
                {
                    throw new Exception("Đầu sách đang được mượn. Không thể xóa!");                    
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi CBLL_DauSach.XoaDauSach \n" + ex.ToString());
            }
        }        

        public bool CapNhatDauSach(CDauSach ds)
        {
            try
            {
                int dgm = dalSach.CountTrangThaiByDauSachID(@"Dang muon", ds.MaDauSach);
                int gh = dalSach.CountTrangThaiByDauSachID(@"Gia han", ds.MaDauSach);

                if (dgm * gh == 0)// nếu chưa có sách nào được mượn hoặc gia hạn
                {
                    int slold = int.Parse(dalDauSach.GetString("SoLuong", ds.MaDauSach));
                    int slnew = ds.SoLuong;
                   //int dm = dalSach.CountTrangThaiByDauSachID(@"Duoc muon", ds.MaDauSach);
                    //int kdm = dalSach.CountTrangThaiByDauSachID(@"Khong duoc muon", ds.MaDauSach);
                    if (slnew == slold)//nếu số lượng không thay đổi
                    {
                        dalSach.UpdateByDauSachID(ds);
                    }
                    else//nếu số lượng thay đổi
                    {
                        dalSach.DeleteByDauSachID(ds.MaDauSach);
                        ThemSach(ds);
                    }
                    dalDauSach.Update(ds);
                    return true;
                }
                else//đã có sách được mượn
                {
                    throw new Exception("Sách đang có người mượn nên không update được");
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi CBLL_DauSach.CapNhatDauSach \n" + ex.ToString());
            }
        }

        public List<CDauSach> LoadData()
        {
            return dalDauSach.GetListAll();
        }

        public List<CSach> LoadSach(string id)
        {
            return dalSach.GetListByDauSachID(id);
        }

        public List<string> LoadTacGia()
        {
            return dalDauSach.GetListString("TacGia");
        }

        public List<string> LoadNhaXB()
        {
            return dalDauSach.GetListString("NhaXB");
        }

        public List<string> LoadNamXB()
        {
            return dalDauSach.GetListString("NamXB");
        }

        public List<string> LoadNgonNgu()
        {
            return dalDauSach.GetListString("NgonNgu");
        }

        public List<string> LoadTheLoai()
        {
            return dalDauSach.GetListString("TheLoai");
        }

        public CDauSach LoadDauSachByDauSachID(string DauSachID)
        {            
            return dalDauSach.GetDauSachByDauSachID(DauSachID);            
        }

        public CDauSach LoadDauSachBySachID(string SachID)
        {
            return dalDauSach.GetDauSachBySachID(SachID);
        }
        
        public List<CDauSach> LoadDauSachByColumnsName(string columnsName, string value)
        {
            return dalDauSach.GetListByColumnsName(columnsName, value);
        }

        public List<CDauSach> LoadDauSachByListColumnsNames(List<string> columnsNames, List<string> values)
        {
            return dalDauSach.GetListByListColumnsNames(columnsNames, values);
        }

        #region: PANDA
        public string timTenSachByMaSach(string maSach)
        {
            try
            {
                return dalSach.GetTenSachByMaSach(maSach);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        public CSach timSachByMaSach(string maSach)
        {
            try
            {
                return dalSach.GetSachByMaSach(maSach);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        public bool UpdateTTSachDangMuon(string maSach)
        {
            try
            {
                return dalSach.UpdateTTDangMuon(maSach);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        public bool UpdateTTTraSach(string maSach)
        {
            try
            {
                return dalSach.UpdateTTTraSach(maSach);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        public bool UpdateTTMatSach(string maSach)
        {
            try
            {
                return dalSach.UpdateTTMatSach(maSach);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        public double GiaTienMatSach(string maSach)
        {
            try
            {
                return dalSach.GiaTienMatSach(maSach);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        public bool UpdateTTHongSach(string maSach, string ghiChu)
        {
            try
            {
                return dalSach.UpdateTTHongSach(maSach, ghiChu);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        public string timTrangThaiDauSach(string dauSachID)
        {
            try
            {
                return dalDauSach.TrangThaiDauSach(dauSachID);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
